package com.yaolonga.itfc.manager.autoconfigure;

import com.yaolonga.itfc.manager.core.*;
import com.yaolonga.itfc.manager.core.utils.SystemUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @Author: yaolong
 * @CreateTime: 2024-10-26  14:22
 * @Description: 接口管理自动配置类
 * @Version: 1.0
 */
@Configuration
@Slf4j
public class InterfaceManagerAutoConfiguration {
    @Bean
    public NetworkInterfaceManager networkInterfaceManager() {
        try {
            if (SystemUtil.isWindows()) {
                String name = System.getProperty("os.name");
                String[] split = name.toLowerCase().split("\\s+");
                if (split.length > 1) {
                    name = split[0];
                }
                String version = System.getProperty("os.version");
                SystemType type = SystemType.of(name);
                return InterfaceManagerRegistry.getInterfaceManager(name, type.name(), version);
            } else if (SystemUtil.isLinux()) {
                Map<String, String> linuxOpSystemInfo = SystemUtil.getLinuxOpSystemInfo();
                String version = linuxOpSystemInfo.get("VERSION_ID");
                String id = linuxOpSystemInfo.get("ID");
                return InterfaceManagerRegistry.getInterfaceManager(PlatformType.linux.name(), id, version);
            }
            return new UnsupportedNetworkInterfaceManager();
        } catch (Exception e) {
            log.error("获取系统类型失败：{}", e.getMessage(), e);
            return new UnsupportedNetworkInterfaceManager();
        }
    }

    public static void main(String[] args) {
        InterfaceManagerAutoConfiguration interfaceManagerAutoConfiguration = new InterfaceManagerAutoConfiguration();
        NetworkInterfaceManager networkInterfaceManager = interfaceManagerAutoConfiguration.networkInterfaceManager();
        System.out.println();
    }
}
